<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>水平垂直居中</title>
    <style>
      .parent {
        position: relative;
        width: 500px;
        height: 500px;
        background-color: #f0f0f0;
      }

      .child {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -50px; /* 高度的一半 */
        margin-left: -50px; /* 宽度的一半 */
        width: 100px;
        height: 100px;
        background-color: #3498db;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="child"></div>
    </div>
  </body>
</html>
